<?php
//define('DEBUG', isset($_REQUEST['debug']) ? TRUE : FALSE);
define('DEBUG', FALSE);
if (DEBUG) {
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
} else {
    error_reporting(E_ERROR);                                       
}
#设置默认时区
date_default_timezone_set ('Asia/Shanghai');
ini_set('oci8.max_persistent', 10);
ini_set('oci8.persistent_timeout', 100);
ini_set('oci8.ping_interval', 60);
require dirname(__FILE__) . '/config/config.conf.php';
require dirname(__FILE__) . '/libraries/Ku6AnalyiticsModel.php';

$_GET = array_merge($_GET, $_POST);
//$_GET = array('c'=>'1234567890123456', 'r'=>'788,608', 'sig'=>'aa26fdeae27a34bc125a236b5e2410f1', 'n'=>0, 'cb'=>'');
$return = array(
    "code" => Ku6ErrorCode::API_EC_SUCCESS, 
    "message" => "",
    "data" => array(
        'cookie_id' => '',
        'total_count' => 0,
        'recommend_id' => '',
        'is_default' => FALSE,
        'entry' => array(
            ),
        ),
);

$msg = '';
$cb = isset($_GET['cb']) ? trim($_GET['cb']) : '';
if (!verifyGet($msg)) {
	$return['message'] = $msg;
	//$return['code'] = Ku6ErrorCode::API_EC_PARAM;
    $return['code'] = 101;
	populateResult($return, $cb);
}

$cookid = trim($_GET['cookie_id']);
$number = isset($_GET['recommend_num']) ? intval($_GET['recommend_num']) : 0;

$anaModel = new Ku6AnalyiticsModel();
if ($anaModel->getErrNo()) {
    $return['code'] = $anaModel->getErrNo();
	$return['message'] = Ku6ErrorCode::getErrMsg($return['code']);
    $return['code'] = 101;
	unset($anaModel);
	populateResult($return, $cb);
}

$visit_ids = $_GET['viewed_list_sorted'];
$vid = getViewingVideoId($_GET['viewed_list_unsorted']);
$result = $anaModel->getVideos($visit_ids, $number, $vid);

$return['data']['cookie_id'] = @ $_GET['cookie_id'];
if ($result===false) {
	$return['code'] = $anaModel->getErrNo();
	$return['message'] = Ku6ErrorCode::getErrMsg($return['code']);
    $return['code'] = 101;
} else {
    //$return['result'] = $result;
    $return['message'] = 'success';
    $return['data']['entry'] = $result;
    $return['data']['is_default'] = FALSE;
    $return['data']['total_count'] = sizeof($result);
    $curTime = microtime(TRUE);
    $curTime = str_replace('.', '', $curTime);
    $return['data']['recommend_id'] = $curTime;
}

/*
if (substr($cookid, -3) == EXPOSURE_COOKID_SUFFIX) {
	$anaModel->logExposure($cookid, $visit_ids, $result);
}
 */
unset($anaModel);
populateResult($return, $cb);
